/*
 *Puppeteer 爬虫 滚动加载更多
 */

const puppeteer = require('puppeteer');

const timestart = new Date().getTime();
// 启动 chrome 
(async() => {
	// 默认启动内嵌的 chrome
	const browser = await puppeteer.launch(
                {args: ['--no-sandbox'],
		 ignoreHTTPSErrors: true}
        );
	// open webpage
	const page = await browser.newPage();
	// set window viewport
	await page.setViewport({width:1440, height:1080});
	await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36');
	await page.goto('http://www.cnbeta.com/category/movie.htm', {waitUntil: 'domcontentloaded'});
	//await sleep(10000);
	// 滚动到页面底部, 并等待窗口高度增加
	// 滚动加载3次
	let lastHeight;
	for (var i=0;i<3;i++){
		lastHeight = await page.evaluate('document.body.offsetHeight');
		await page.evaluate('window.scrollTo(0, 99999999)')
		await page.waitForFunction((lastHeight)=>{
			return document.body.offsetHeight - lastHeight > 200;
		}, {}, lastHeight);
	}
	// 检查列表长度
	const listHdl = await page.$x('//div[@class="cnbeta-update-list category-update"]/div[@class="items-area"]/div[@class="item"]');
	console.log('list items: ' + listHdl.length);
	//await page.screenshot({fullPage:true, path: '/home/linglq/Desktop/chroma/full.png'});
	
	await browser.close();
	
	const timeend = new Date().getTime();
	console.log(timestart, timeend, timeend - timestart);
})();

function sleep(time){
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve();
        }, time);
    })
}
